﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;

/// <summary>
/// Author:0
/// Created Date:10-Feb-2011
/// Description:Used to Read and Update the XML Data.
/// </summary>
namespace EncircleFTM
{
    public class clsSettings
    {
        #region "Static Variables"

        public static bool IsConfigFileExists = false;
        public static string CompanyName = string.Empty;
        public static string UniqueID = string.Empty;
        public static string URL = string.Empty;
        public static string UploadURL = string.Empty;
        public static string FolderPath = string.Empty;
        public static string APP_PATH = clsCommonData.GetAppPath();
        public static string APP_DATA_PATH = clsCommonData.GetAppDataPath();
        public static string Patient_ID = string.Empty;
        public static string Service_Date = string.Empty;
        public static string Provider_Name = string.Empty;
        public static string Document_Type = string.Empty;
        public static string Procedure_1 = string.Empty;
        public static string Procedure_2 = string.Empty;
        public static string Note_1 = string.Empty;
        public static string Note_2 = string.Empty;
        public static clsCommonData.ClearLogSchedule objClearLogSchedule = clsCommonData.ClearLogSchedule.None;
        public static DateTime lastClearedDate;
        public static int lastClearDay;
        public static int lastClearMonth;
        public static int lastClearYear;
        public static int lastClearHour;
        public static int lastClearMin;
        public static int lastClearSec;
        public static string ISAdditionalInfo;
        public static string machineName = System.Environment.MachineName;
        public static string domainname = System.Environment.UserName;
        public const string XML_NODE_COMP_NAME = @"/Settings/CompanyName";
        public const string XML_NODE_COMP_ID = @"/Settings/UniqueID";
        public const string XML_NODE_COMP_URL = @"/Settings/URL";
        public const string XML_NODE_COMP_UPLOADURL = @"/Settings/UploadURL";
        public const string XML_NODE_COMP_FOLDERPATH = @"/Settings/FolderPath";
        public const string XML_NODE_COMP_CLEARLOG_SCHEDULE = @"/Settings/ClearLogSchedule";
        public const string XML_NODE_COMP_LAST_CLEARDATE = @"/Settings/LastClearedDate";
        public const string XML_NODE_COMP_LAST_CLEARDAY = @"/Settings/LastClearDay";
        public const string XML_NODE_COMP_LAST_CLEARMONTH = @"/Settings/LastClearMonth";
        public const string XML_NODE_COMP_LAST_CLEARYEAR = @"/Settings/LastClearYear";
        public const string XML_NODE_COMP_LAST_CLEARHOUR = @"/Settings/LastClearHour";
        public const string XML_NODE_COMP_LAST_CLEARMIN = @"/Settings/LastClearMin";
        public const string XML_NODE_COMP_LAST_CLEARSEC = @"/Settings/LastClearSec";
        public const string XML_NODE_COMP_ADDITIONAL_INFO = @"/Settings/Additionalinfo";
        public const string XML_NODE_ADDINFO_PROCEDURE1 = @"/AdditionalInformation/Procedure1";
        public const string XML_NODE_ADDINFO_PROCEDURE2 = @"/AdditionalInformation/Procedure2";
        public const string XML_NODE_ADDINFO_NOTE1 = @"/AdditionalInformation/Note1";
        public const string XML_NODE_ADDINFO_NOTE2 = @"/AdditionalInformation/Note2";
        public const string XML_NODE_ADDINFO_SERVICEDATE = @"/AdditionalInformation/ServiceDate";
        public const string XML_NODE_ADDINFO_PROVIDER = @"/AdditionalInformation/Provider";
        public const string XML_NODE_ADDINFO_PATIENTID = @"/AdditionalInformation/PatientID";
        public const string XML_NODE_ADDINFO_DOCUMENTTYPE = @"/AdditionalInformation/DocumentType";
        public const string XML_NODE_STATICLIST_PATIENTID = @"/STATICLIST/MostRecent";
        public const string XML_NODE_STATICLIST_PROVIDER = @"/STATICLIST/Provider";
        public const string XML_NODE_STATICLIST_PROCEDURE1 = @"/STATICLIST/Procedure1";
        public const string XML_NODE_STATICLIST_PROCEDURE2 = @"/STATICLIST/Procedure2";
        public const string XML_NODE_STATICLIST_NOTE1 = @"/STATICLIST/Note1";
        public const string XML_NODE_STATICLIST_NOTE2 = @"/STATICLIST/Note2";

        #endregion
        #region"Methods"
        /// <summary>
        /// To read the User Details
        /// </summary>
        public static bool ReadSettings()
        {

            bool isconfigFileExists = false;
            IsConfigFileExists = false;
            try
            {
                //Change Nov 27 2012
                //Create folder on application path with hostname and folder name
                //string UserFolderpath = Path.Combine(APP_PATH, machineName + domainname);
             //   string UserFolderpath = Path.Combine(APP_DATA_PATH, machineName + domainname);
                  string UserFolderpath = APP_DATA_PATH;

                //If Folder exists copy config file to the User folder                               
                if (!Directory.Exists(UserFolderpath))
                {
                    Directory.CreateDirectory(UserFolderpath);
                    File.Copy(Path.Combine(APP_PATH, clsCommonData.STR_CONFIG_XML), Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML));
                    if (!File.Exists(Path.Combine(UserFolderpath, clsCommonData.STR_ADDITIONAL_XML)))
                    {
                        //If not exists copy file to the location
                        File.Copy(Path.Combine(APP_PATH, clsCommonData.STR_ADDITIONAL_XML), Path.Combine(UserFolderpath, clsCommonData.STR_ADDITIONAL_XML));
                    }
                    if (!File.Exists(Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML)))
                    {
                        //If not exists copy file to the location
                        File.Copy(Path.Combine(APP_PATH, clsCommonData.STR_STATICLIST_XML), Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML));
                    }
                }
                else
                {
                    //Check Config file Exists                    
                    if (!File.Exists(Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML)))
                    {
                        //If not exists copy file to the location
                        File.Copy(Path.Combine(APP_PATH, clsCommonData.STR_CONFIG_XML), Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML));
                    }
                    else
                    {
                        DateTime globalConfigModifiedTime = new FileInfo(Path.Combine(APP_PATH, clsCommonData.STR_CONFIG_XML)).LastWriteTime;
                        DateTime localConfigModifiedTime = new FileInfo(Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML)).LastWriteTime;
                        //Overwriting the local Url's in config.xml if the global Url's config.xml is latest.
                        if (globalConfigModifiedTime >= localConfigModifiedTime)
                        {
                            XmlDocument globalXml = new XmlDocument();
                            XmlDocument localXml = new XmlDocument();
                            globalXml.Load(Path.Combine(APP_PATH, clsCommonData.STR_CONFIG_XML));
                            localXml.Load(Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML));
                            localXml.SelectSingleNode(XML_NODE_COMP_URL).InnerText = globalXml.SelectSingleNode(XML_NODE_COMP_URL).InnerText;
                            localXml.SelectSingleNode(XML_NODE_COMP_UPLOADURL).InnerText = globalXml.SelectSingleNode(XML_NODE_COMP_UPLOADURL).InnerText;                            
                            localXml.Save(Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML));
                        }
                    }

                }

                XmlDocument local_Xml = new XmlDocument();
                local_Xml.Load(Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML));
                local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARDAY).InnerText = local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARDAY).InnerText.Equals("0") ? DateTime.Today.Day.ToString() : local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARDAY).InnerText;
                local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARDATE).InnerText = local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARDATE).InnerText.Equals("0") ? DateTime.Now.ToString("MM/dd/yyyy") : local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARDATE).InnerText;
                local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARHOUR).InnerText = local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARHOUR).InnerText.Equals("0") ? DateTime.Now.Hour.ToString() : local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARHOUR).InnerText;
                local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARMIN).InnerText = local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARMIN).InnerText.Equals("0") ? DateTime.Now.Minute.ToString() : local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARMIN).InnerText;
                local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARMONTH).InnerText = local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARMONTH).InnerText.Equals("0") ? DateTime.Now.Month.ToString() : local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARMONTH).InnerText;
                local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARSEC).InnerText = local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARSEC).InnerText.Equals("0") ? DateTime.Now.Second.ToString() : local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARSEC).InnerText;
                local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARYEAR).InnerText = local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARYEAR).InnerText.Equals("0") ? DateTime.Now.Year.ToString() : local_Xml.SelectSingleNode(XML_NODE_COMP_LAST_CLEARYEAR).InnerText;
                local_Xml.Save(Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML));

                //Set Folder path for this user in this config file in settings screen
                //For fodler wathcher get File path from this config file                
                if (File.Exists(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML)))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML));

                    CompanyName = doc.SelectSingleNode(XML_NODE_COMP_NAME).InnerText;
                    UniqueID = doc.SelectSingleNode(XML_NODE_COMP_ID).InnerText;
                    URL = doc.SelectSingleNode(XML_NODE_COMP_URL).InnerText;
                    UploadURL = doc.SelectSingleNode(XML_NODE_COMP_UPLOADURL).InnerText;
                    FolderPath = doc.SelectSingleNode(XML_NODE_COMP_FOLDERPATH).InnerText;
                    objClearLogSchedule = (clsCommonData.ClearLogSchedule)Enum.Parse(typeof(clsCommonData.ClearLogSchedule), doc.SelectSingleNode(XML_NODE_COMP_CLEARLOG_SCHEDULE).InnerText);

                    lastClearedDate = Convert.ToDateTime(doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARDATE).InnerText);
                    lastClearDay = Convert.ToInt32(doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARDAY).InnerText);
                    lastClearMonth = Convert.ToInt32(doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARMONTH).InnerText);
                    lastClearYear = Convert.ToInt32(doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARYEAR).InnerText);
                    lastClearHour = Convert.ToInt32(doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARHOUR).InnerText);
                    lastClearMin = Convert.ToInt32(doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARMIN).InnerText);
                    lastClearSec = Convert.ToInt32(doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARSEC).InnerText);
                    ISAdditionalInfo = Convert.ToString(doc.SelectSingleNode(XML_NODE_COMP_ADDITIONAL_INFO).InnerText);
                    doc = null;
                    isconfigFileExists = true;
                    IsConfigFileExists = true;
                }
                else
                {
                    clsCommonData.WriteError(clsCommonData.MSG_FAILURE);
                    isconfigFileExists = false;
                    IsConfigFileExists = false;
                }
            }
            catch (Exception ex)
            {

                clsCommonData.WriteError(ex.ToString());
                isconfigFileExists = false;
                IsConfigFileExists = false;
            }
            return isconfigFileExists;
        }


        /// <summary>
        /// To read the User Details
        /// </summary>
        public static bool ReadAdditionalInfo()
        {
            bool isADDInfoFileExists = false;
            isADDInfoFileExists = false;
            try
            {
                //Change Nov 27 2012


                //Create folder on application path with hostname and folder name
                //string UserFolderpath = Path.Combine(APP_PATH, machineName + domainname);
                //string UserFolderpath = Path.Combine(APP_DATA_PATH, machineName + domainname);
                string UserFolderpath = APP_DATA_PATH;
                //If Folder exists copy config file to the User folder
                if (!Directory.Exists(UserFolderpath))
                {
                    Directory.CreateDirectory(UserFolderpath);
                    File.Copy(Path.Combine(APP_PATH, clsCommonData.STR_ADDITIONAL_XML), Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML));
                }
                else
                {
                    //Check Config file Exists                    
                    if (!File.Exists(Path.Combine(UserFolderpath, clsCommonData.STR_ADDITIONAL_XML)))
                    {
                        //If not exists copy file to the location
                        File.Copy(Path.Combine(APP_PATH, clsCommonData.STR_ADDITIONAL_XML), Path.Combine(UserFolderpath, clsCommonData.STR_ADDITIONAL_XML));
                    }
                }

                //Set Folder path for this user in this config file in settings screen
                //For fodler wathcher get File path from this config file

                if (File.Exists(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_ADDITIONAL_XML)))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_ADDITIONAL_XML));

                    Provider_Name = doc.SelectSingleNode(XML_NODE_ADDINFO_PROVIDER).InnerText;
                    Patient_ID = doc.SelectSingleNode(XML_NODE_ADDINFO_PATIENTID).InnerText;
                    Document_Type = doc.SelectSingleNode(XML_NODE_ADDINFO_DOCUMENTTYPE).InnerText;
                    Service_Date = doc.SelectSingleNode(XML_NODE_ADDINFO_SERVICEDATE).InnerText;
                    Procedure_1 = doc.SelectSingleNode(XML_NODE_ADDINFO_PROCEDURE1).InnerText;
                    Procedure_2 = doc.SelectSingleNode(XML_NODE_ADDINFO_PROCEDURE2).InnerText;
                    Note_1 = doc.SelectSingleNode(XML_NODE_ADDINFO_NOTE1).InnerText;
                    Note_2 = doc.SelectSingleNode(XML_NODE_ADDINFO_NOTE2).InnerText;
                    isADDInfoFileExists = true;
                }
                else
                {
                    clsCommonData.WriteError(clsCommonData.MSG_FAILURE);
                    isADDInfoFileExists = false;
                    isADDInfoFileExists = false;

                }
            }
            catch (Exception ex)
            {

                clsCommonData.WriteError(ex.ToString());
                isADDInfoFileExists = false;
                isADDInfoFileExists = false;
            }
            return isADDInfoFileExists;
        }
        /// <summary>
        /// Read Static information list
        /// </summary>
        /// <returns></returns>
        public static string ReadStaticListInfo()
        {

            string Patient_ID = string.Empty;
            try
            {
                //Create folder on application path with hostname and folder name                
                //string UserFolderpath = Path.Combine(APP_PATH, machineName + domainname);
               // string UserFolderpath = Path.Combine(APP_DATA_PATH, machineName + domainname);
                string UserFolderpath = APP_DATA_PATH;
                //If Folder exists copy config file to the User folder

                if (!Directory.Exists(UserFolderpath))
                {
                    Directory.CreateDirectory(UserFolderpath);
                    File.Copy(Path.Combine(APP_PATH, clsCommonData.STR_STATICLIST_XML), Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML));
                }
                else
                {
                    //Check Config file Exists                    
                    if (!File.Exists(Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML)))
                    {
                        //If not exists copy file to the location
                        File.Copy(Path.Combine(APP_PATH, clsCommonData.STR_STATICLIST_XML), Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML));
                    }
                }

                //Set Folder path for this user in this config file in settings screen
                //For fodler wathcher get File path from this config file

                if (File.Exists(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML)))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML));

                    Patient_ID = doc.SelectSingleNode(XML_NODE_STATICLIST_PATIENTID).InnerText;
                }
                else
                {
                    clsCommonData.WriteError(clsCommonData.MSG_FAILURE);
                    //MessageBox.Show(clsCommonData.MSG_FAILURE, clsCommonData.STR_APPNAME);
                }
            }
            catch (Exception ex)
            {
                clsCommonData.WriteError(ex.ToString());
            }
            return Patient_ID;
        }
        /// <summary>
        /// Read Static information by Type
        /// </summary>
        /// <param name="Type"></param>
        /// <returns></returns>
        public static string ReadStaticListInfo(string Type)
        {
            string XML_NODE_SEARCH = string.Empty;
            if (Type == clsCommonData.STR_STATIC_PROVIDER)
            {
                XML_NODE_SEARCH = XML_NODE_STATICLIST_PROVIDER;
            }
            else if (Type == clsCommonData.STR_STATIC_PROCEDURE1)
            {
                XML_NODE_SEARCH = XML_NODE_STATICLIST_PROCEDURE1;
            }
            else if (Type == clsCommonData.STR_STATIC_PROCEDURE2)
            {
                XML_NODE_SEARCH = XML_NODE_STATICLIST_PROCEDURE2;
            }
            else if (Type == clsCommonData.STR_STATIC_NOTE1)
            {
                XML_NODE_SEARCH = XML_NODE_STATICLIST_NOTE1;
            }
            else if (Type == clsCommonData.STR_STATIC_NOTE2)
            {
                XML_NODE_SEARCH = XML_NODE_STATICLIST_NOTE2;
            }
            string Patient_ID = string.Empty;
            try
            {
                //Create folder on application path with hostname and folder name
                //string UserFolderpath = Path.Combine(APP_PATH, machineName + domainname);
               // string UserFolderpath = Path.Combine(APP_DATA_PATH, machineName + domainname);
                //If Folder exists copy config file to the User folder
                string UserFolderpath = APP_DATA_PATH;

                if (!Directory.Exists(UserFolderpath))
                {
                    Directory.CreateDirectory(UserFolderpath);
                    File.Copy(Path.Combine(APP_PATH, clsCommonData.STR_STATICLIST_XML), Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML));
                }
                else
                {
                    //Check Config file Exists                    
                    if (!File.Exists(Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML)))
                    {
                        //If not exists copy file to the location
                        File.Copy(Path.Combine(APP_PATH, clsCommonData.STR_STATICLIST_XML), Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML));
                    }
                }

                //Set Folder path for this user in this config file in settings screen
                //For fodler wathcher get File path from this config file

                if (File.Exists(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML)))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML));

                    Patient_ID = doc.SelectSingleNode(XML_NODE_SEARCH).InnerText;
                }
                else
                {
                    clsCommonData.WriteError(clsCommonData.MSG_FAILURE);
                    //MessageBox.Show(clsCommonData.MSG_FAILURE, clsCommonData.STR_APPNAME);
                }
            }
            catch (Exception ex)
            {
                clsCommonData.WriteError(ex.ToString());
            }
            return Patient_ID;
        }
        /// <summary>
        /// Update Additional information
        /// </summary>
        /// <param name="PatientID"></param>
        /// <param name="servicedate"></param>
        /// <param name="DocumentType"></param>
        /// <param name="Provider"></param>
        /// <param name="Procedure1"></param>
        /// <param name="Procedure2"></param>
        /// <param name="Note1"></param>
        /// <param name="Note2"></param>
        /// <returns></returns>
        public static bool UpdateAdditionalInfo(string PatientID, DateTime servicedate, string DocumentType, string Provider, string Procedure1, string Procedure2, string Note1, string Note2)
        {
            bool retval = false;
            try
            {
                //Change Nov 27 2012

                //string UserFolderpath = Path.Combine(APP_PATH, machineName + domainname);
             //  string UserFolderpath = Path.Combine(APP_DATA_PATH, machineName + domainname);
                string UserFolderpath = APP_DATA_PATH;

                //Check Config file Exists                    
                if (!File.Exists(Path.Combine(UserFolderpath, clsCommonData.STR_ADDITIONAL_XML)))
                {
                    //If not exists copy file to the location
                    File.Copy(Path.Combine(APP_PATH, clsCommonData.STR_ADDITIONAL_XML), Path.Combine(UserFolderpath, clsCommonData.STR_ADDITIONAL_XML));
                }

                if (File.Exists(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_ADDITIONAL_XML)))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_ADDITIONAL_XML));
                    doc.SelectSingleNode(XML_NODE_ADDINFO_PROVIDER).InnerText = Provider;
                    doc.SelectSingleNode(XML_NODE_ADDINFO_PATIENTID).InnerText = PatientID;
                    doc.SelectSingleNode(XML_NODE_ADDINFO_DOCUMENTTYPE).InnerText = DocumentType;
                    doc.SelectSingleNode(XML_NODE_ADDINFO_PROCEDURE1).InnerText = Procedure1;
                    doc.SelectSingleNode(XML_NODE_ADDINFO_PROCEDURE2).InnerText = Procedure2;
                    doc.SelectSingleNode(XML_NODE_ADDINFO_SERVICEDATE).InnerText = servicedate.ToString("MM/dd/yyyy");
                    doc.SelectSingleNode(XML_NODE_ADDINFO_NOTE1).InnerText = Note1;
                    doc.SelectSingleNode(XML_NODE_ADDINFO_NOTE2).InnerText = Note2;
                    doc.Save(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_ADDITIONAL_XML));
                    doc = null;
                    retval = true;
                }

            }
            catch (Exception ex)
            {
                clsCommonData.WriteError(ex.ToString());
            }
            return retval;
        }
        /// <summary>
        /// Updatestatic list
        /// </summary>
        /// <param name="_PatientID"></param>
        /// <returns></returns>
        public static bool UpdateStaticListInfo(string _PatientID)
        {
            bool retval = false;
            try
            {

                //string UserFolderpath = Path.Combine(APP_PATH, machineName + domainname);
             //   string UserFolderpath = Path.Combine(APP_DATA_PATH, machineName + domainname);
                string UserFolderpath = APP_DATA_PATH;

                if (File.Exists(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML)))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML));
                    doc.SelectSingleNode(XML_NODE_STATICLIST_PATIENTID).InnerText = _PatientID;
                    doc.Save(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML));
                    doc = null;
                    retval = true;
                }

            }
            catch (Exception ex)
            {
                clsCommonData.WriteError(ex.ToString());
            }
            return retval;
        }
        /// <summary>
        /// Updatestatic list
        /// </summary>
        /// <param name="_PatientID"></param>
        /// <returns></returns>
        public static bool UpdateStaticListInfo(string Value, string type)
        {
            string STR_SEARCH_NODE = string.Empty;
            if (type == "Provider")
            {
                STR_SEARCH_NODE = XML_NODE_STATICLIST_PROVIDER;
            }
            else if (type == "Procedure1")
            {
                STR_SEARCH_NODE = XML_NODE_STATICLIST_PROCEDURE1;
            }
            else if (type == "Procedure2")
            {
                STR_SEARCH_NODE = XML_NODE_STATICLIST_PROCEDURE2;
            }
            else if (type == "Note1")
            {
                STR_SEARCH_NODE = XML_NODE_STATICLIST_NOTE1;
            }
            else if (type == "Note2")
            {
                STR_SEARCH_NODE = XML_NODE_STATICLIST_NOTE2;
            }
            bool retval = false;
            try
            {

               // string UserFolderpath = Path.Combine(APP_PATH, machineName + domainname);
              //  string UserFolderpath = Path.Combine(APP_DATA_PATH, machineName + domainname);
                string UserFolderpath = APP_DATA_PATH;

                if (File.Exists(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML)))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML));
                    if (doc.SelectSingleNode(STR_SEARCH_NODE).InnerText == string.Empty)
                    {
                        doc.SelectSingleNode(STR_SEARCH_NODE).InnerText = Value;
                    }
                    else
                    {
                        doc.SelectSingleNode(STR_SEARCH_NODE).InnerText += "~" + Value;
                    }
                    doc.Save(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_STATICLIST_XML));
                    doc = null;
                    retval = true;
                }

            }
            catch (Exception ex)
            {
                clsCommonData.WriteError(ex.ToString());
            }
            return retval;
        }
        /// <summary>
        /// To get the Application Path.
        /// </summary>
        /// <returns></returns>
        public static string GetAppPath()
        {
            string path = string.Empty;
            try
            {
                path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
                return path;
            }
            catch (Exception ex)
            {
                clsCommonData.WriteError(ex.ToString());
            }
            return path;
        }

        /// <summary>
        /// To save the User Details.
        /// </summary>
        /// <param name="pCompanyName">Name of the Company</param>
        /// <param name="pUniqueID">UniqueID of the Company</param>
        /// <param name="pURL">URL of the Company</param>
        /// <param name="pFolderPath">Folder Path to watch</param>
        /// <returns></returns>
        public static bool UpdateSettings(string pCompanyName, string pUniqueID, string pURL, string pUploadURL, string pFolderPath, clsCommonData.ClearLogSchedule objClearLogSch, Boolean AdditionalInfo)
        {
            bool retval = false;
            try
            {
                //Change Nov 27 2012

               // string UserFolderpath = Path.Combine(APP_PATH, machineName + domainname);
              //  string UserFolderpath = Path.Combine(APP_DATA_PATH, machineName + domainname);
                string UserFolderpath = APP_DATA_PATH;

                if (File.Exists(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML)))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML));
                    doc.SelectSingleNode(XML_NODE_COMP_NAME).InnerText = pCompanyName;
                    doc.SelectSingleNode(XML_NODE_COMP_ID).InnerText = pUniqueID;
                    doc.SelectSingleNode(XML_NODE_COMP_URL).InnerText = pURL;
                    doc.SelectSingleNode(XML_NODE_COMP_UPLOADURL).InnerText = pUploadURL;
                    doc.SelectSingleNode(XML_NODE_COMP_FOLDERPATH).InnerText = pFolderPath;
                    doc.SelectSingleNode(XML_NODE_COMP_CLEARLOG_SCHEDULE).InnerText = objClearLogSch.ToString();
                    doc.SelectSingleNode(XML_NODE_COMP_ADDITIONAL_INFO).InnerText = AdditionalInfo.ToString();
                    doc.Save(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML));
                    doc = null;
                    retval = true;
                }

            }
            catch (Exception ex)
            {
                clsCommonData.WriteError(ex.ToString());
            }
            return retval;
        }

        /// <summary>
        /// Used to update last log cleared date and time.
        /// </summary>
        /// <param name="lastClearLogDate"></param>
        /// <returns></returns>
        public static bool UpdateClearLogDate(int Day, int Month, int Year, int Hour, int Min, int sec)
        {
            bool retval = false;
            try
            {
                //string UserFolderpath = Path.Combine(APP_PATH, machineName + domainname);
                //string UserFolderpath = Path.Combine(APP_DATA_PATH, machineName + domainname);
                string UserFolderpath = APP_DATA_PATH;

                if (File.Exists(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML)))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML));
                    doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARDATE).InnerText = new DateTime(Year, Month, Day, Hour, Min, sec).ToString("MM/dd/yyyy");
                    doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARDAY).InnerText = Day.ToString();
                    doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARMONTH).InnerText = Month.ToString();
                    doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARYEAR).InnerText = Year.ToString();
                    doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARHOUR).InnerText = Hour.ToString();
                    doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARMIN).InnerText = Min.ToString();
                    doc.SelectSingleNode(XML_NODE_COMP_LAST_CLEARSEC).InnerText = sec.ToString();

                    doc.Save(System.IO.Path.Combine(UserFolderpath, clsCommonData.STR_CONFIG_XML));
                    retval = true;
                    doc = null;
                }
            }
            catch (Exception ex)
            {
                clsCommonData.WriteError(ex.ToString());
            }
            return retval;
        }
        #endregion
    }


}

